package com.example.demo.utils;

import com.example.demo.entity.CSDNArticleEntity;
import com.example.demo.entity.CSDNAuthorEntity;
import okhttp3.OkHttpClient;

import java.util.Calendar;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * csdn爬虫的一些设置
 * */
public class CSDNSpiderUtils {

    // 设置一个List用来记录还没有消费的url
    public static ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(2048);

    // 配置一个okhttp客户端
    public static OkHttpClient client = new OkHttpClient()
            .newBuilder()
            .connectTimeout(1000, TimeUnit.SECONDS)
            .build();




    // 计算权重信息
    public static double getScore(int read_volum, int collection_volum, String up_time, double authorscore) {
        double x1 = 0.4;
        double x2 = 0.4;
        double x3 = 0.1;
        double x4 = 0.1;
        double score = x1*read_volum/1000 + x2*collection_volum;
        Calendar calendar = Calendar.getInstance();
        Integer year = Integer.parseInt(String.valueOf(calendar.get(Calendar.YEAR)));
        score += x3*(1/(year - Integer.parseInt(up_time.split("-")[0]) + 1));
        score += x4*authorscore;
        return score;
    }

    // 计算作者得分
    public static double getAuthorScore(Integer num_fans, Integer num_like, Integer num_comment, Integer num_collect) {
        double x1 = 0.2;
        double x2 = 0.2;
        double x3 = 0.3;
        double x4 = 0.3;
        double score = x1*num_fans + x2*num_like + x3*num_comment + x4*num_collect;
        score = score / 1000;
        return score;
    }

    // 这个函数进行分数过滤，只有大于阈值的信息才能被记录
    private static final double threshold1 = 1.0;
    private static final double threshold2 = 0.01;
    public static boolean scoreFilter(CSDNArticleEntity articleEntity, CSDNAuthorEntity authorEntity) {
        if(articleEntity==null || authorEntity==null) {
            return false;
        }
        if(articleEntity.getScore()<threshold1 || authorEntity.getAuthor_score()<threshold2) {
            return false;
        }
        return true;
    }


}
